define(function(require, exports, module) {

	var utils = require("utils");

	var cometdDojo = {};

	// --- cometdDojo -----
	cometdDojo.connect = function(config, onLoad, onUnload) {
		window.dojo.addOnLoad(function() {
			onLoad();
			window.dojox.cometd.init({
				url : config.url,
				connectTimeout : config.connectTimeout,
				maxConnections : config.maxConnections,
				backoffIncrement : config.backoffIncrement,
				logLevel : config.logLevel,
				maxBackoff : config.maxBackoff,
				reverseIncomingExtensions : config.reverseIncomingExtensions,
				maxNetworkDelay : config.maxNetworkDelay,
				appendMessageTypeToURL : config.appendMessageTypeToURL,
				autoBatch : config.autoBatch,
				requestHeaders : {},
				advice : {
					timeout : 60000,
					interval : 0,
					reconnect : 'retry'
				}
			});
		});
		window.dojo.addOnUnload(function() {
			onUnload();
			window.dojox.cometd.disconnect(true);
		});
	};

	// --- cometdDojo -----
	cometdDojo.disconnect = function() {
		window.dojox.cometd.disconnect(true);
	};

	// --- cometdDojo -----
	cometdDojo.isDisconnected = function() {
		return window.dojox.cometd.isDisconnected();
	};

	// --- cometdDojo -----
	cometdDojo.batch = function(callback) {
		return window.dojox.cometd.batch(callback);
	};

	// --- cometdDojo -----
	cometdDojo.addListener = function(channel, listener) {
		return window.dojox.cometd.addListener(channel, listener);
	};

	// --- cometdDojo -----
	cometdDojo.addMetaListener = function(channel, listener) {
		return window.dojox.cometd.addListener("/meta/" + channel, listener);
	};

	// --- cometdDojo -----
	cometdDojo.removeListener = function(listener) {
		return window.dojox.cometd.removeListener(listener);
	};

	// --- cometdDojo -----
	cometdDojo.subscribe = function(channel, onReceiveMessage, subscribeProps,
			subscribeCallback) {
		return window.dojox.cometd.subscribe(channel, onReceiveMessage,
				subscribeProps, subscribeCallback);
	};

	// --- cometdDojo -----
	cometdDojo.publish = function(channel, data) {
		return window.dojox.cometd.publish(channel, data);
	};

	// --- cometdDojo -----
	cometdDojo.unsubscribe = function(subscription) {
		return window.dojox.cometd.unsubscribe(subscription);
	};

	// --- cometdDojo -----
	cometdDojo.disableWebSocketTransport = function() {
		return window.dojox.cometd.websocketEnabled = false;
	};

	// ------------------------------------------------------------------------------------------

	var cometdRPC = require('bean/cometAjaxRpcService');

	var cometdClient = {};

	cometdClient.clientId = null;
	cometdClient.isConnect = false;
	cometdClient._connected = false;
	cometdClient.isHandshake = false;
	cometdClient.userChannelUrl = "/user-channel";
	cometdClient.uniqueChannelPrefix = "/unique-channel";
	cometdClient.circleChannelPrefix = "/circle-channel";
	cometdClient.userId = null;
	
	cometdClient.onReadyEventList = new Array();
	cometdClient.subscribeSuccessSet = new Array();
	
	// ---
	cometdClient.createCometdConfig = function(url) {
		cometdConfig = {};
		cometdConfig.url = url;
		cometdConfig.connectTimeout = 0;
		cometdConfig.maxConnections = 2;
		cometdConfig.backoffIncrement = 1000;
		cometdConfig.logLevel = "info";
		cometdConfig.maxBackoff = 60000;
		cometdConfig.reverseIncomingExtensions = true;
		cometdConfig.maxNetworkDelay = 10000;
		cometdConfig.appendMessageTypeToURL = true;
		cometdConfig.autoBatch = false;
		return cometdConfig;
	};

	// ---
	cometdClient.isReady = function() {
		return this.isConnect && this.isHandshake;
	};

	// ---
	cometdClient.connect = function(url) {

		cometdDojo.connect(this.createCometdConfig(url), function() {

			cometdDojo.addMetaListener("connect", function(message) {
		
				var wasConnected = cometdClient._connected;
				cometdClient._connected = message.successful;
				cometdClient.isConnect = cometdClient._connected;
				console.log(cometdClient._connected);
				
				if (!wasConnected && cometdClient._connected) {

					console.log("connect And reConnected ... ");

				} else if (wasConnected && !cometdClient._connected) {

					console.log("Disconnect... ");

				} else {

				}

			});

		}, function() {

			console.log("stop...");

		});

	};

	// ---
	cometdClient.connectUserChannel = function(userId) {

	};

	// ---
	cometdClient.getClientId = function() {

	};

	// ----
	cometdClient.addOnReadyEventCallback = function(callback) {

	};

	// -------------------------------------------------------------------------------------------

	module.exports = cometdClient;

});